﻿using Microsoft.SharePoint.Workflow;

namespace PD.ShP.WorkflowActivities
{
    /// <summary>
    /// Provides extension methods for SPWorkflowTaskProperties type.
    /// </summary>
    public static class SPWorkflowTaskPropertiesHelper
    {
        /// <summary>
        /// Determines whether the task is approved based on the specified properties.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <returns>
        /// <c>true</c> if has been approved; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsTaskApproved(this SPWorkflowTaskProperties properties)
        {
            return properties.ExtendedProperties["TaskStatus"] != null
                && properties.ExtendedProperties["TaskStatus"] as string == "#";
        }

        /// <summary>
        /// Gets the comment typed by user.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <returns>
        /// The comment typed by user.
        /// </returns>
        public static string GetComment(this SPWorkflowTaskProperties properties)
        {
            return properties.ExtendedProperties["Comments"] as string;
        }

        /// <summary>
        /// Sets the allow delegation value.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <param name="allowDelegation">if set to <c>true</c> if the task can be delegated to other users.</param>
        public static void SetAllowDelegation(this SPWorkflowTaskProperties properties, bool allowDelegation)
        {
            properties.ExtendedProperties["ows_AllowDelegation"] = allowDelegation.ToString();
        }

        /// <summary>
        /// Sets the allow change requests value.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <param name="allowChangeRequests">if set to <c>true</c> if the change requests are allowed.</param>
        public static void SetAllowChangeRequests(this SPWorkflowTaskProperties properties, bool allowChangeRequests)
        {
            properties.ExtendedProperties["ows_AllowChangeRequests"] = allowChangeRequests.ToString();
        }
    }
}
